package cn.tedu.egvehicle.base.interceptors;

import cn.tedu.egvehicle.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("进入登陆状态校验的拦截器");
        //获取会话
        /*
            1.得到session会话对象;
              HttpSession session = request.getSession();
            2.从会话对象中获取用户标识;
              2.1 获取成功: 放行return true;
              2.2 获取失败:
                  重定向到登录页面;
                    response.sendRedirect("/login.html");
                  拦截return false;
         */
        HttpSession session = request.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null ){
            response.sendRedirect("login.html");
            return false;
        }
        return true;
    }
}
